package com.yunshang.shoppingmall.Wechat;

import com.yunshang.shoppingmall.common.LayUIUtil;
import com.yunshang.shoppingmall.entity.WeChatMu;
import com.yunshang.shoppingmall.service.WeChat.WeChatMuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RequestMapping(value = "/WeChat")
@Controller
public class weChatMuController {

    @Autowired
    WeChatMuService weChatMuService;

    //添加页面
    @RequestMapping("/Mu")
    public ModelAndView Mu() {
        ModelAndView modelAndView = new ModelAndView("WeChat/Mu/List");
        return modelAndView;
    }

    //列表Json数据
    @ResponseBody
    @RequestMapping("/JsonMuList")
    public LayUIUtil JsonMuList() {
        List<WeChatMu> muList=weChatMuService.selectMuList();
        if(muList!=null) {
            return LayUIUtil.data(muList.size(), muList);
        }
        else
        {
            return LayUIUtil.data(0, null);
        }
    }

    //添加页面
    @RequestMapping("/Mu/Add")
    public ModelAndView Add() {
        ModelAndView modelAndView = new ModelAndView("WeChat/Mu/Add");
        return modelAndView;
    }

    //添加页面
    @ResponseBody
    @RequestMapping("/Mu/AddMu")
    public int AddMu(@RequestParam Map<String,String> map, HttpServletRequest request) {
        String muName = map.get("muName");
        String muUrl = map.get("muUrl");
        String sort1 = map.get("sort");
        int sort = Integer.parseInt(sort1);
        WeChatMu weChatMu = new WeChatMu();
        weChatMu.setMuName(muName);
        weChatMu.setMuUrl(muUrl);
        weChatMu.setSort(sort);
        int row = weChatMuService.insertSelective(weChatMu);
        return row;
    }

    //编辑页面
    @RequestMapping("/Mu/Edit")
    public ModelAndView Edit(@RequestParam Map<String,String> map, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("WeChat/Mu/Edit");
        String id1 = map.get("id");
        int id = Integer.parseInt(id1);
        WeChatMu weChatMu = weChatMuService.selectByPrimaryKey(id);
        if(weChatMu!=null) {
            modelAndView.addObject("weChatMu", weChatMu);
        }
        return modelAndView;
    }

    //添加页面
    @ResponseBody
    @RequestMapping("/Mu/EditMu")
    public int EditMu(@RequestParam Map<String,String> map, HttpServletRequest request) {
        String id1 = map.get("id");
        int id = Integer.parseInt(id1);
        String muName = map.get("muName");
        String muUrl = map.get("muUrl");
        String sort1 = map.get("sort");
        int sort = Integer.parseInt(sort1);
        WeChatMu weChatMu = new WeChatMu();
        weChatMu.setId(id);
        weChatMu.setMuName(muName);
        weChatMu.setMuUrl(muUrl);
        weChatMu.setSort(sort);
        int row = weChatMuService.updateByPrimaryKeySelective(weChatMu);
        return row;
    }

}